package com.gitee.baqkft.code.mybatis;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

/**
 * Mybatis注释生成器
 * @Author: chenmingcai
 * @Date 2020/5/31 9:50 上午
 **/

public class FriendlyCommentGenerator extends DefaultCommentGenerator
{

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
    {
        topLevelClass.addJavaDocLine("/**");
        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for(int i = 0; i < remarkLines.length; ++i) {
                String remarkLine = remarkLines[i];
                topLevelClass.addJavaDocLine(" *   " + remarkLine);
            }
        }

        topLevelClass.addJavaDocLine(" *");
        StringBuilder sb = new StringBuilder();
        sb.append(" * database table: ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addFieldComment(org.mybatis.generator.api.dom.java.Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn)
    {
        String remarks = introspectedColumn.getRemarks();
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + remarks);
        field.addJavaDocLine(" */");
    }
}
